﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LeEdu.MODEL;
using System.Data.Entity;
using System.Data;
using System.Linq.Expressions;
namespace LeEdu.DAL
{
   public class BaseDAL<T>where T:class
    {
       DbContextEntity entity = new DbContextEntity();
       /// <summary>
       /// 添加
       /// </summary>
       /// <param name="t">对象</param>
       /// <returns></returns>
       public bool AddEntity(T t)
       {
           entity.Entry<T>(t).State = EntityState.Added;
           return entity.SaveChanges() > 0;
       }
       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="t">对象</param>
       /// <returns></returns>
       public bool Del(T t)
       {
           entity.Set<T>().Attach(t);
           entity.Entry<T>(t).State = EntityState.Deleted;
           return entity.SaveChanges() > 0;
       }
       /// <summary>
       /// 修改
       /// </summary>
       /// <param name="t"></param>
       /// <returns></returns>
       public bool Edit(T t)
       {
           entity.Set<T>().Attach(t);
           entity.Entry<T>(t).State = EntityState.Modified;
           return entity.SaveChanges() > 0;
       }
       /// <summary>
       /// 获取一条数据
       /// </summary>
       /// <param name="whereLambda">Lambda表达式</param>
       /// <returns></returns>
       public T getEntity(Expression<Func<T, bool>> whereLambda)
       {
           return entity .Set<T>().FirstOrDefault(whereLambda);
       }
       /// <summary>
       /// 获取对象的集合
       /// </summary>
       /// <param name="whereLambda">Lambda表达式</param>
       /// <returns></returns>
       public List<T> getEntityList(Expression<Func<T, bool>> whereLambda)
       {
           return entity.Set<T>().ToList();
       }
       /// <summary>
       /// 根据编号查一条数据
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public T Search(int id)
       {
           return entity.Set<T>().Find(id);
       }

       /// <summary>
       /// 获取集合信息
       /// </summary>
       /// <returns></returns>
       public virtual List<T> GetAll()
       {
           var list = entity.Set<T>().ToList();
           return list;
       }

       /// <summary>
       /// 分页
       /// </summary>
       /// <typeparam name="A">排序字段</typeparam>
       /// <param name="orderLambda">排序条件</param>
       /// <param name="WhereLambda">查询条件</param>
       /// <param name="pageindex">页码</param>
       /// <param name="pagesize">每页条数</param>
       /// <param name="pageCount">总页数</param>
       /// <param name="total">总条数</param>
       /// <param name="isasc">排序方式（默认升序）</param>
       /// <returns>对象集合（List<T>）</returns>
       public virtual List<T> getEntityPagging<A>(Expression<Func<T, A>> orderLambda, Expression<Func<T, bool>> WhereLambda, int pageindex, int pagesize, out int total, out int pageCount, bool isasc = true)
       {
           //算出总页数方法一
           //total = db.Set<T>().Count();
           //pageCount = total / pagesize + total % pagesize == 0 ? 0 : 1;

           //算出总页数方法二
           total = entity.Set<T>().Where(WhereLambda).Count();

           pageCount = Convert.ToInt32(Math.Ceiling(total / (decimal)pagesize));

           if (isasc)
           {
               return entity.Set<T>().OrderBy(orderLambda).Where(WhereLambda).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
           }
           else
           {
               return entity.Set<T>().OrderByDescending(orderLambda).Where(WhereLambda).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
           }

       }
       //查询
       public IQueryable<Courses> chaxun(System.Linq.Expressions.Expression<Func<Courses,bool>>wherelambda)
       {

           return entity.Set<Courses>().Where(wherelambda);

       }

       //使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
       public string DateStringFromNow(DateTime dt)
       {
           TimeSpan span = DateTime.Now - dt;
           if (span.TotalDays > 60)
           {
               return dt.ToShortDateString();
           }
           else
           {
               if (span.TotalDays > 30)
               {
                   return
                   "1个月前";
               }
               else
               {
                   if (span.TotalDays > 14)
                   {
                       return
                       "2周前";
                   }
                   else
                   {
                       if (span.TotalDays > 7)
                       {
                           return
                           "1周前";
                       }
                       else
                       {
                           if (span.TotalDays > 1)
                           {
                               return
                               string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
                           }
                           else
                           {
                               if (span.TotalHours > 1)
                               {
                                   return
                                   string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
                               }
                               else
                               {
                                   if (span.TotalMinutes > 1)
                                   {
                                       return
                                       string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
                                   }
                                   else
                                   {
                                       if (span.TotalSeconds >= 1)
                                       {
                                           return
                                           string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
                                       }
                                       else
                                       {
                                           return
                                           "1秒前";
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
    }
}
